package com.shu.map;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/8/4 16:19
 */

public class HashMap {

    public static void main(String[] args) {
        new HashMap().test();
    }

    private transient Entry[] table;

    public void test() {
        Entry c = new Entry("c", null);
        Entry b = new Entry("b", c);
        Entry a = new Entry("a", b);
        table = new Entry[4];
        table[1] = a;
        System.out.println("原始链表：" + a.toString());
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Entry[] newTable = new Entry[8];
                for (Entry e : table) {
                    if (null != e) {
                        System.out.println("线程1让出CPU时间片前,操作的entry为：" + e.toString());
                    }
                    while (null != e) {
                        Entry next = e.next;
                        //产生死循环主要在这里，第一次遍历上述的next为 b->c->null,
                        // 下一次遍历 b时，由于线程2修改成了 c->b->a->null导致 b 节点的next又重新指向 a节点
                        //线程1让出CPU时间片,等线程二操作完
                        sleep(5000);
                        System.out.println("线程1继续执行,操作的entry为：" + e.toString());
                        e.next = newTable[1];
                        newTable[1] = e;
//                        System.out.println("执行后的entry为："+e.toString());
                        System.out.println("=============================");
                        e = next;
                    }
                }
                table = newTable;
            }
        });
        thread.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Entry[] newTable = new Entry[8];
                for (Entry e : table) {
                    if (null != e) {
                        System.out.println("线程2开始,操作的entry为：" + e.toString());
                    }
                    while (null != e) {
                        sleep(500);
                        Entry next = e.next;
                        e.next = newTable[1];
                        newTable[1] = e;
                        e = next;
                    }
                }
                System.out.println("线程2扩容完,entry为：" + newTable[1].toString());
                sleep(6000);
                table = newTable;
            }
        }).start();
        sleep(20000);
        System.out.println("2个线程扩容完毕后,最终entry为：" + table[1].value + "->" + table[1].next.value + "->" + table[1].next.next.value);
    }

    private void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void transfer(Entry[] newTable) {

    }

    private int indexFor(int hash, int length) {
        return hash & (length - 1);
    }

    ///////////// HashMap节点移动相关逻辑 //////////////////
//    void transfer(Entry[] newTable, boolean rehash) {
//        int newCapacity = newTable.length;
//        for (Entry<K,V> e : table) {
//            while(null != e) {
//                Entry<K,V> next = e.next;
//                if (rehash) {
//                    e.hash = null == e.key ? 0 : hash(e.key);
//                }
//                int i = indexFor(e.hash, newCapacity);
//                e.next = newTable[i];
//                newTable[i] = e;
//                e = next;
//            }
//        }
//    }

    static class Entry {

        private String value;

        private Entry next;

        private int hash = 1;

        public Entry(String value, Entry next) {
            this.value = value;
            this.next = next;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public Entry getNext() {
            return next;
        }

        public void setNext(Entry next) {
            this.next = next;
        }

        @Override
        public String toString() {
            if (null != value) {

                return value + "->" + (null == next ? null : next.toString());
            }
            return null;
        }
    }
}
